home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************************************
- IsoHex22_1.cpp
- Ernest S. Pazera
- 22NOV2000
- Start a WIN32 Application Workspace, add in this file
- Requires GDICanvas.h/cpp
- No other libs are required
- *****************************************************************************/
-
- //////////////////////////////////////////////////////////////////////////////
- //INCLUDES
- //////////////////////////////////////////////////////////////////////////////
- #define WIN32_LEAN_AND_MEAN
-
- #include <windows.h>
- #include <stdlib.h>
- #include "gdicanvas.h"
-
- //////////////////////////////////////////////////////////////////////////////
- //DEFINES
- //////////////////////////////////////////////////////////////////////////////
- //name for our window class
- #define WINDOWCLASS "ISOHEX22"
- //title of the application
- #define WINDOWTITLE "IsoHex 22-1"
-
- //maze constants
- //size
- const int MAZE_WIDTH=16;
- const int MAZE_HEIGHT=16;
-
- //directions
- const int MAZE_NORTH=0;
- const int MAZE_EAST=1;
- const int MAZE_SOUTH=2;
- const int MAZE_WEST=3;
- const int MAZE_DOORCOUNT=4;
-
- //room structure
- struct Room
- {
- //one door for each direction
- bool Door[MAZE_DOORCOUNT];
- };
-
- //////////////////////////////////////////////////////////////////////////////
- //PROTOTYPES
- //////////////////////////////////////////////////////////////////////////////
- bool Prog_Init();//game data initalizer
- void Prog_Loop();//main game loop
- void Prog_Done();//game clean up
- void MakeMaze();//make the maze
- int CountDoors(Room* pRoom);//count the doors in a room
- void DrawMaze();//draw the maze
-
- //////////////////////////////////////////////////////////////////////////////
- //GLOBALS
- //////////////////////////////////////////////////////////////////////////////
- HINSTANCE hInstMain=NULL;//main application handle
- HWND hWndMain=NULL;//handle to our main window
-
- //the maze
- Room Maze[MAZE_WIDTH][MAZE_HEIGHT];
-
- //tileset for showing the maze(not a real tileset)
- CGDICanvas gdicMaze;
-
- //////////////////////////////////////////////////////////////////////////////
- //WINDOWPROC
- //////////////////////////////////////////////////////////////////////////////
- LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
- {
- //which message did we get?
- switch(uMsg)
- {
- case WM_DESTROY://the window is being destroyed
- {
-
- //tell the application we are quitting
- PostQuitMessage(0);
-
- //handled message, so return 0
- return(0);
-
- }break;
- case WM_PAINT://the window needs repainting
- {
- //a variable needed for painting information
- PAINTSTRUCT ps;
-
- //start painting
- HDC hdc=BeginPaint(hwnd,&ps);
-
- //end painting
- EndPaint(hwnd,&ps);
-
- //draw the maze
- DrawMaze();
-
- //handled message, so return 0
- return(0);
- }break;
- }
-
- //pass along any other message to default message handler
- return(DefWindowProc(hwnd,uMsg,wParam,lParam));
- }
-
-
- //////////////////////////////////////////////////////////////////////////////
- //WINMAIN
- //////////////////////////////////////////////////////////////////////////////
- int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
- {
- //assign instance to global variable
- hInstMain=hInstance;
-
- //create window class
- WNDCLASSEX wcx;
-
- //set the size of the structure
- wcx.cbSize=sizeof(WNDCLASSEX);
-
- //class style
- wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
-
- //window procedure
- wcx.lpfnWndProc=TheWindowProc;
-
- //class extra
- wcx.cbClsExtra=0;
-
- //window extra
- wcx.cbWndExtra=0;
-
- //application handle
- wcx.hInstance=hInstMain;
-
- //icon
- wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
-
- //cursor
- wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
-
- //background color
- wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
-
- //menu
- wcx.lpszMenuName=NULL;
-
- //class name
- wcx.lpszClassName=WINDOWCLASS;
-
- //small icon
- wcx.hIconSm=NULL;
-
- //register the window class, return 0 if not successful
- if(!RegisterClassEx(&wcx)) return(0);
-
- //create main window
- hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_BORDER | WS_SYSMENU | WS_CAPTION| WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
-
- //error check
- if(!hWndMain) return(0);
-
- //if program initialization failed, then return with 0
- if(!Prog_Init()) return(0);
-
- //message structure
- MSG msg;
-
- //message pump
- for(;;)
- {
- //look for a message
- if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
- {
- //there is a message
-
- //check that we arent quitting
- if(msg.message==WM_QUIT) break;
-
- //translate message
- TranslateMessage(&msg);
-
- //dispatch message
- DispatchMessage(&msg);
- }
-
- //run main game loop
- Prog_Loop();
- }
-
- //clean up program data
- Prog_Done();
-
- //return the wparam from the WM_QUIT message
- return(msg.wParam);
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //INITIALIZATION
- //////////////////////////////////////////////////////////////////////////////
- bool Prog_Init()
- {
- //set the client area size
- RECT rcTemp;
- SetRect(&rcTemp,0,0,256,256);//256x256 client area
- AdjustWindowRect(&rcTemp,WS_BORDER | WS_SYSMENU | WS_CAPTION| WS_VISIBLE,FALSE);//adjust the window size based on desired client area
- SetWindowPos(hWndMain,NULL,0,0,rcTemp.right-rcTemp.left,rcTemp.bottom-rcTemp.top,SWP_NOMOVE);//set the window width and height
-
- //load in the maze image
- gdicMaze.Load(NULL,"mazets.bmp");
-
- //make the maze
- MakeMaze();
-
- return(true);//return success
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //CLEANUP
- //////////////////////////////////////////////////////////////////////////////
- void Prog_Done()
- {
- //////////////////////////
- //clean up code goes here
- //////////////////////////
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //MAIN GAME LOOP
- //////////////////////////////////////////////////////////////////////////////
- void Prog_Loop()
- {
- ///////////////////////////
- //main game logic goes here
- ///////////////////////////
- }
-
- void MakeMaze()
- {
- //start making the maze
- //assumes the maze array is clean (i.e. all values are false)
- int doorcount=0;//number of doors we have place
- //x and y position of the room
- int x;
- int y;
- int dir;//direction
- //randomize the seed
- srand(GetTickCount());
- //clear out the maze
- for(x=0;x<MAZE_WIDTH;x++)
- for(y=0;y<MAZE_HEIGHT;y++)
- for(dir=0;dir<MAZE_DOORCOUNT;dir++)
- Maze[x][y].Door[dir]=false;
- while (doorcount<MAZE_WIDTH*MAZE_HEIGHT-1)
- {
- DrawMaze();
- //this flag is set if a suitable room is found
- bool found=false;
- //blocks, used a little later
- int blockcount;
- bool block[MAZE_DOORCOUNT];
- while(!found)
- {
- //if doorcount is 0, just pick a room
- if(doorcount==0)
- {
- x=rand()%MAZE_WIDTH;
- y=rand()%MAZE_HEIGHT;
- }
- else
- {
- //doorcount not zero, pick a room with a door in it
- do
- {
- x=rand()%MAZE_WIDTH;
- y=rand()%MAZE_HEIGHT;
- } while (CountDoors(&Maze[x][y])==0);
- //CountDoors is pseudocode for a function we'll write later
- }
- //so far, so good, we have a room
- //we now must analyze blocked passages
- blockcount=0;
- //loop through directions
- for(dir=0;dir<MAZE_DOORCOUNT;dir++)
- {
- //set block to false
- block[dir]=false;
- //if the door in the room for dir is set, set the block
- if(Maze[x][y].Door[dir])
- {
- block[dir]=true;
- blockcount++;
- continue;
- }
- //calculate the next position based on this direction
- int nx=x,ny=y;//next position
- switch(dir)
- {
- case MAZE_NORTH:
- {
- ny--;
- }break;
- case MAZE_EAST:
- {
- nx++;
- }break;
- case MAZE_SOUTH:
- {
- ny++;
- }break;
- case MAZE_WEST:
- {
- nx--;
- }break;
- }
- //bounds checking
- if(nx<0 || ny<0 || nx>=MAZE_WIDTH || ny>=MAZE_HEIGHT)
- {
- block[dir]=true;
- blockcount++;
- continue;
- }
- //final check, make sure the room at nx,ny has no doors
- if(CountDoors(&Maze[nx][ny])!=0)
- {
- block[dir]=true;
- blockcount++;
- continue;
- }
- }
- //if blockcount is not the same as MAZE_DOORCOUNT, we can place a door
- if(blockcount!=MAZE_DOORCOUNT) found=true;
- }
- //found is now true, so we can place a door
- do
- {
- //select a door direction
- dir=rand()%MAZE_DOORCOUNT;
- }while (block[dir]);//make sure that direction is not blocked
- //dir contains a valid direction
- Maze[x][y].Door[dir]=true;
- //move to next room
- switch(dir)
- {
- case MAZE_NORTH:
- {
- y--;
- }break;
- case MAZE_EAST:
- {
- x++;
- }break;
- case MAZE_SOUTH:
- {
- y++;
- }break;
- case MAZE_WEST:
- {
- x--;
- }break;
- }
- //change direction to opposite direction
- dir=(dir+(MAZE_DOORCOUNT/2)) % MAZE_DOORCOUNT;
- //set door in the next room
- Maze[x][y].Door[dir]=true;
- //increment the doorcount
- doorcount++;
- }
- }
-
- int CountDoors(Room* pRoom)//count the doors in a room
- {
- //initialize total to 0
- int total=0;
- //start counting doors
- for(int dir=0;dir<MAZE_DOORCOUNT;dir++)
- {
- //if door is fount, add one to the total
- if(pRoom->Door[dir]) total++;
- }
- //return total
- return(total);
- }
-
- void DrawMaze()
- {
- //grab dc from window
- HDC hdc=GetDC(hWndMain);
- //loop variables
- int x;
- int y;
- //image stuff
- int imagex;
- int imagey;
- int imagenum;
- //loop through columns
- for(x=0;x<MAZE_WIDTH;x++)
- {
- //loop through rows
- for(y=0;y<MAZE_HEIGHT;y++)
- {
- //put together the image number
- imagenum=0;//initialize to 0
- //north, add 1
- if(Maze[x][y].Door[MAZE_NORTH]) imagenum+=1;
- //east, add 2
- if(Maze[x][y].Door[MAZE_EAST]) imagenum+=2;
- //south, add 4
- if(Maze[x][y].Door[MAZE_SOUTH]) imagenum+=4;
- //west, add 8
- if(Maze[x][y].Door[MAZE_WEST]) imagenum+=8;
- //calculate the imagex and imagey
- imagex=(imagenum%8)*16;
- imagey=(imagenum/8)*16;
- //blit the appropriate image
- BitBlt(hdc,x*16,y*16,16,16,gdicMaze,imagex,imagey,SRCCOPY);
- }
- }
- //release the dc
- ReleaseDC(hWndMain,hdc);
- }
-